#include "HttpServer.hpp"
#include "HttpProtocol.hpp"

void SetCookieExpires(HttpResponse& resp)
{
    resp.SetHeader("Set-Cookie", "username=zhangsan; expires=" + resp.ExpireTimeUseRfc1123(600) + ";");
}

void SetCookiePath(HttpResponse& resp)
{
    std::string value = "username=zhangsan; expires=" + resp.ExpireTimeUseRfc1123(600) + ";"
     + " path=/register.html;";
    resp.SetHeader("Set-Cookie", value);
}

void Login(HttpRequest& req, HttpResponse& resp,  HttpServer& hsvr)
{
    // 根据req构建resp
    LOG(LogLevel::INFO) << "进入登录模块," << " path: " << req.Path() << ", args: " << req.Args();
    
    std::string content = req.GetContent("wwwroot/success.html");
    resp.SetCode(200);
    resp.SetHeader("Content-Length", std::to_string(content.size()));
    resp.SetHeader("Content-Type", "text/html");

    SetCookieExpires(resp);
    // SetCookiePath(resp);
    // resp.SetHeader("Set-Cookie", req.Args());
    resp.SetContent(content);

}

void Register(HttpRequest& req, HttpResponse& resp)
{
    // 根据req构建resp
    LOG(LogLevel::INFO) << "进入注册模块," << " path: " << req.Path() << ", args: " << req.Args();
}

void Search(HttpRequest& req, HttpResponse& resp)
{
    // 根据req构建resp
    LOG(LogLevel::INFO) << "进入搜索模块," << " path: " << req.Path() << ", args: " << req.Args();
}

void Test(HttpRequest& req, HttpResponse& resp)
{
    // 根据req构建resp
    LOG(LogLevel::INFO) << "进入测试模块," << " path: " << req.Path() << ", args: " << req.Args();
}


int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }
    auto httpserver = std::make_unique<HttpServer>(std::stoi(argv[1]));
    httpserver->Register("/login", Login);
    // httpserver->Register("/register", Register);
    // httpserver->Register("/search", Search);
    // httpserver->Register("/test", Test);

    httpserver->Start();
    return 0;
}